/**
 * Project Name:Micro-Services-Common
 * File Name   :JdkLoggerAdapter.java
 * Package Name:com.micro.services.common.logger.jdk
 * Date:2015年9月8日上午12:50:16
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package com.micro.services.common.logger.jdk;

import java.io.File;

import com.micro.services.common.logger.Level;
import com.micro.services.common.logger.Logger;
import com.micro.services.common.logger.LoggerAdapter;

/**
 * ClassName:JdkLoggerAdapter <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason :TODO ADD REASON. <br/>
 * Date :2015年9月8日 上午12:50:16 <br/>
 * 
 * @author :http://my.oschina.net/httpssl
 * @email :491835898@QQ.COM
 * @since :JDK 1.7
 * @see
 */
public class JdkLoggerAdapter implements LoggerAdapter
{
	private File file;

	@Override
	public Logger getLogger(Class<?> key) {
		// TODO Auto-generated method stub
		return new JdkLogger(java.util.logging.Logger.getLogger(key == null ? "" : key.getName()));
	}

	@Override
	public Logger getLogger(String key) {
		// TODO Auto-generated method stub
		return new JdkLogger(java.util.logging.Logger.getLogger(key));
	}

	@Override
	public void setLevel(Level level) {
		// TODO Auto-generated method stub
		java.util.logging.Logger.getGlobal().setLevel(toJdkLevel(level));
	}

	@Override
	public Level getLevel() {
		// TODO Auto-generated method stub
		return fromJdkLevel(java.util.logging.Logger.getGlobal().getLevel());
	}

	@Override
	public File getFile() {
		// TODO Auto-generated method stub
		return file;
	}

	@Override
	public void setFile(File file) {
		// TODO Auto-generated method stub
		this.file = file;
	}

	private static java.util.logging.Level toJdkLevel(Level level) {
		if (level == Level.ALL)
			return java.util.logging.Level.ALL;
		if (level == Level.TRACE)
			return java.util.logging.Level.FINER;
		if (level == Level.DEBUG)
			return java.util.logging.Level.FINE;
		if (level == Level.INFO)
			return java.util.logging.Level.INFO;
		if (level == Level.WARN)
			return java.util.logging.Level.WARNING;
		if (level == Level.ERROR)
			return java.util.logging.Level.SEVERE;
		// if (level == Level.OFF)
		return java.util.logging.Level.OFF;
	}

	private static Level fromJdkLevel(java.util.logging.Level level) {
		if (level == java.util.logging.Level.ALL)
			return Level.ALL;
		if (level == java.util.logging.Level.FINER)
			return Level.TRACE;
		if (level == java.util.logging.Level.FINE)
			return Level.DEBUG;
		if (level == java.util.logging.Level.INFO)
			return Level.INFO;
		if (level == java.util.logging.Level.WARNING)
			return Level.WARN;
		if (level == java.util.logging.Level.SEVERE)
			return Level.ERROR;

		return Level.OFF;
	}
}
